package com.aerozhonghuan.logisticsmall.framework.hybrid.actions;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.aerozhonghuan.logisticsmall.framework.base.MyAppliation;
import com.aerozhonghuan.logisticsmall.framework.hybrid.NewWebviewFragment;
import com.aerozhonghuan.logisticsmall.framework.hybrid.SimpleActionHandler;
import com.aerozhonghuan.logisticsmall.framework.hybrid.bean.JsMobileAgentConstants;
import com.aerozhonghuan.hybrid.XJsCallback;
import com.aerozhonghuan.location.GeoReverseCallBack;
import com.aerozhonghuan.location.ReverseGeoCodeUtils;
import com.aerozhonghuan.location.ZhLocationCallback;
import com.aerozhonghuan.location.ZhLocationUtils;
import com.aerozhonghuan.location.bean.ReverseCodeBean;
import com.aerozhonghuan.location.bean.ZhLocationBean;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述
 * 作者 Zhang Yonghui
 * 创建日期 2019/1/21
 */
public class LocationActionHandler extends SimpleActionHandler {
    private static final String KEY_LONGITUDE = "longitude";
    private static final String KEY_LATITUDE = "latitude";
    private static final String KEY_ADDRESS = "address";
    private ZhLocationUtils mZhLocationUtils;

    public LocationActionHandler(NewWebviewFragment fragment) {
        super(JsMobileAgentConstants.FLAG_LOCATION, fragment);
    }

    @Override
    public void handleAction(@NonNull String action, @Nullable JSONObject params, @Nullable final XJsCallback callBack) {
        if (mZhLocationUtils == null) {
            mZhLocationUtils = new ZhLocationUtils();
        }
        mZhLocationUtils.startLocation(MyAppliation.getApplication(), new ZhLocationCallback() {
            @Override
            public void onSuccess(ZhLocationBean zhLocationBean) {
                ReverseGeoCodeUtils reverseGeoCodeUtils = new ReverseGeoCodeUtils();
                reverseGeoCodeUtils.startGeoReverse(zhLocationBean.lat, zhLocationBean.lon, new GeoReverseCallBack() {
                    @Override
                    public void onReverseGeoCodeSuccess(ReverseCodeBean reverseCodeBean) {
                        if (reverseCodeBean != null) {
                            Map<String, String> resultParams = new HashMap<>();
                            resultParams.put(KEY_LONGITUDE, String.valueOf(reverseCodeBean.lon));
                            resultParams.put(KEY_LATITUDE, String.valueOf(reverseCodeBean.lat));
                            resultParams.put(KEY_ADDRESS, reverseCodeBean.address);
                            onResult(resultParams, callBack);
                        } else {
                            if (callBack != null) {
                                callBack.failure(0, "获取地理位置失败");
                            }
                        }
                    }

                    @Override
                    public void onReverseGeoCodeFail(String s) {
                        if (callBack != null) {
                            callBack.failure(0, "逆地理失败");
                        }
                    }
                });
            }

            @Override
            public void onFailure() {
                if (callBack != null) {
                    callBack.failure(0, "定位失败");
                }
            }
        });

    }
}
